//
//  ZGLSubDetailsVC.m
//  Faker
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLSubDetailsVC.h"
#import "ZGLSubDetCollectionViewCell.h"
#import "ZGLSubDetModel.h"
#import "ZGLMoveView.h"
#import "ZGLNewsDetViewController.h"


@interface ZGLSubDetailsVC () <UICollectionViewDataSource, UICollectionViewDelegate, ZGLSubDetCollectionViewCellDelegate> {
    NSString *signPlist;
}

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *modelArr;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) ZGLMoveView *moveView;
@property (nonatomic, strong) UIButton *subButton;


@property (nonatomic, strong) NSMutableArray *signArr;


@end

@implementation ZGLSubDetailsVC

- (void)dealloc {
    _collectionView.delegate = nil;
    _collectionView.dataSource = nil;
    [_backButton removeTarget:self action:@selector(backButton:) forControlEvents:(UIControlEventTouchUpInside)];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        _signArr = [user arrayForKey:@"currentArr"].mutableCopy;
    }
    return self;
}

- (void)setNavigationBar {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zaihufanhui"] style:(UIBarButtonItemStyleDone) target:self action:@selector(leftBarButtonItem:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor whiteColor];
}
- (void)leftBarButtonItem:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    self.moveView.backgroundColor = naviColor;
    self.navigationController.navigationBarHidden= YES;
    self.tabBarController.tabBar.hidden = YES;
}
- (void)setSubButton {
    _subButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_subButton setFrame:CGRectMake(ScaleX * 375 / 3, ScaleY * 623.5, ScaleX * 375 / 3, ScaleY * 40)];
    _subButton.backgroundColor = naviColor;
    _subButton.layer.cornerRadius = 5;
    _subButton.layer.masksToBounds = YES;
    [_subButton setTitle:@"现在订阅" forState:(UIControlStateNormal)];
    [_subButton setTitle:@"取消订阅" forState:(UIControlStateSelected)];
    [_subButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [_subButton setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateSelected)];
    [_subButton addTarget:self action:@selector(subButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_subButton];
}
- (void)subButton:(UIButton *)button {
    if (button.selected) {
        button.selected = !button.selected;
        [self delegateLocalSignplist];
    } else {
        button.selected = !button.selected;
        [self writeLocalSignplist];
    }
}

- (void)writeLocalSignplist {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    self.signArr = [userDefaults arrayForKey:@"currentArr"].mutableCopy;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:_yuanDic[@"title"] forKey:@"title"];
    [dic setObject:_yuanDic[@"large_pic"] forKey:@"large_pic"];
    [dic setObject:_yuanDic[@"block_color"] forKey:@"block_color"];
    [dic setObject:_yuanDic[@"api_url"] forKey:@"api_url"];
    [dic setObject:_yuanDic[@"data_type"] forKeyedSubscript:@"data_type"];
    [self.signArr addObject:dic];
    [userDefaults setObject:_signArr forKey:@"currentArr"];
    [userDefaults synchronize];
}

- (void)delegateLocalSignplist {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    self.signArr = [userDefaults arrayForKey:@"currentArr"].mutableCopy;
    NSMutableArray *newArr = [NSMutableArray arrayWithCapacity:0];
    for (NSDictionary *dic in _signArr) {
        if ([dic[@"title"] isEqualToString:_yuanDic[@"title"]]) {
            
        } else {
            [newArr addObject:dic];
        }
    }
    [userDefaults setObject:newArr forKey:@"currentArr"];
    [userDefaults synchronize];
}



- (void)setYuanDic:(NSDictionary *)yuanDic {
    if (_yuanDic != yuanDic) {
        _yuanDic = yuanDic;
    }
    for (NSDictionary *dic in _signArr) {
        if ([yuanDic[@"title"] isEqualToString:dic[@"title"]]) {
            [_subButton setSelected:YES];
            break;
        } else {
            [_subButton setSelected:NO];
        }
    }
}


- (void)createCollectionView {
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.scrollDirection = 1;
    flow.itemSize = CGSizeMake(ScaleX * 375, ScaleY * 647);
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, ScaleX * 375, ScaleY * 647) collectionViewLayout:flow];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.backgroundColor = [UIColor grayColor];
    [self.collectionView registerClass:[ZGLSubDetCollectionViewCell class]
    forCellWithReuseIdentifier:@"ZGLSubDetailsVC_ZGLSubDetCollectionViewCell"];
}

- (void)createBackButton {
    self.moveView = [[ZGLMoveView alloc] initWithFrame:CGRectMake(ScaleX * 20, ScaleY * 620, ScaleY * 47 , ScaleY * 47)];
    self.moveView.layer.cornerRadius = ScaleY * 23.5;
    self.moveView.layer.masksToBounds = YES;
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backButton setFrame:CGRectMake(ScaleX * 11, ScaleY * 11, ScaleY * 25, ScaleY * 25)];
    self.backButton.layer.cornerRadius = ScaleY * 12.5;
    self.backButton.layer.masksToBounds = YES;
    [self.backButton setImage:[UIImage imageNamed:@"zaihufanhui"] forState:(UIControlStateNormal)];
    [self.backButton setSelected:YES];
    [self.backButton setImage:[UIImage imageNamed:@"fanhuizhuye"] forState:(UIControlStateSelected)];
    [self.backButton addTarget:self action:@selector(backButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.moveView addSubview:self.backButton];
    [self.view addSubview:self.moveView];
    self.view.multipleTouchEnabled = YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (2 == [touches count]) {
    }
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    /* 接收集合中的所有元素 */
    NSArray *arr = [touches allObjects];
    /* 取出对象中的对象 UITouch */
    UITouch *touch = [arr objectAtIndex:0];
    /* 获取touch 在view 上的位置 */
    CGPoint location = [touch locationInView:self.view];
    /* 将moveView的中心点等于location */
    if (!(touch.view == self.view)) {
        self.moveView.center = location;
    }
}
- (void)backButton:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
   
    [self setNavigationBar];
    [self createCollectionView];
    [self createBackButton];
    [self setSubButton];
    // Do any additional setup after loading the view.
}

- (void)setDetListArr:(NSMutableArray *)detListArr {
    if (_detListArr != detListArr) {
        _detListArr = detListArr;
    }
    NSMutableArray *arr= [NSMutableArray arrayWithCapacity:0];
    NSMutableArray *arr1 = [NSMutableArray arrayWithCapacity:0];
    
    for (ZGLSubDetailsVC *model in detListArr) {
        [arr addObject:model];
        if (arr.count == 6) {
            [arr1 addObject:arr];
            arr = [NSMutableArray arrayWithCapacity:0];
        }
    }
    _modelArr = arr1;

    [self.collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (_modelArr.count == 0) {
        return 2;
    } else {
        return _modelArr.count;
    }
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZGLSubDetCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZGLSubDetailsVC_ZGLSubDetCollectionViewCell" forIndexPath:indexPath];
    cell.delegate = self;
    cell.titleUrl = _titleUrl;
    cell.detListArr = _modelArr[indexPath.item];
    if (_modelArr.count) {
        cell.allPage = _modelArr.count;
        cell.currentPage = indexPath.item + 1;
    }
    return cell;
}

- (void)pass:(ZGLSubDetModel *)model {
    ZGLNewsDetViewController *newsVC = [[ZGLNewsDetViewController alloc] init];
    
    [self presentViewController:newsVC animated:YES completion:^{
        newsVC.subModel = model;
    }];
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
